// src/stores/borrow.js

import { defineStore } from 'pinia';

export const useBorrowStore = defineStore('borrow', {
    state: () => ({
        borrowList: [], // 当前的借阅列表
        historyList: [], // 借阅历史记录
    }),

    actions: {
        // 获取借阅列表
        async fetchBorrowList() {
            try {
                // 模拟 API 调用
                this.borrowList = [
                    { id: 1, status: 'PENDING', expectedReturnDate: '2025-06-30' },
                    { id: 2, status: 'APPROVED', expectedReturnDate: '2025-06-15' },
                    { id: 3, status: 'RETURNED', expectedReturnDate: '2025-06-01' },
                ];
                console.log('借阅列表获取成功');
            } catch (error) {
                console.error('获取借阅列表失败:', error);
                throw error;
            }
        },

        // 获取借阅历史记录
        async fetchBorrowHistory() {
            try {
                // 模拟 API 调用
                this.historyList = [
                    { id: 1, book: 'Vue.js Guide', borrowedDate: '2025-06-01', returnedDate: '2025-06-10' },
                    { id: 2, book: 'JavaScript Patterns', borrowedDate: '2025-05-15', returnedDate: '2025-05-20' },
                ];
                console.log('借阅历史获取成功');
            } catch (error) {
                console.error('获取借阅历史失败:', error);
                throw error;
            }
        },

        // 归还书籍
        async returnBook(borrowId) {
            try {
                // 模拟 API 调用
                this.borrowList = this.borrowList.map(item =>
                    item.id === borrowId ? { ...item, status: 'RETURNED' } : item
                );
                console.log(`书籍归还成功，借阅 ID: ${borrowId}`);
            } catch (error) {
                console.error('归还书籍失败:', error);
                throw error;
            }
        },

        // 导出历史记录
        exportHistory() {
            try {
                // 模拟导出逻辑
                console.log('借阅历史导出成功:', this.historyList);
                alert('借阅历史已成功导出！');
            } catch (error) {
                console.error('导出借阅历史失败:', error);
                throw error;
            }
        },
    },
});
